package upload

import (
	"common/encryp/path"
	"context"
	"errors"
	"euplink/center_rpc"
	"go.uber.org/zap"
	"io"
)

func (up *Uploader) UploadInline(ctx context.Context, bucket, originKey string, inReader io.Reader) error {
	up.bucket = bucket

	result := &EncParam{}
	var err error
	result.encrypKey, result.plainKey, err = path.EncPath(originKey, up.apiKey.RootKey()) //todo 不应用rootkey，用derived key
	if err != nil {
		return err
	}

	data, err := io.ReadAll(inReader)
	if len(data) > 16*1e6 { // > 16m
		return errors.New("too big file")
	}

	//请求服务器，获取节点信息
	_, err = up.centerRpc.BeginObject(ctx, center_rpc.BeginObjectParam{
		EncryptedObjectKey:  result.encrypKey,
		EncryptedContentKey: []byte("1"),
		Bucket:              up.bucket,
		InlineData:          data,
	})
	if err != nil {
		up.Log.Error("BeginObject ", zap.Error(err))
		return err
	}
	return nil
}
